package com.jy.adscm.operation.xjfile.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.xjfile.bean.XjFile;
import com.jy.adscm.operation.xjfile.service.XjFileService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-05-07
 */
@RestController
@RequestMapping("/adscm/operation/xjfile/xjFile")
public class XjFileController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    XjFileService xjFileService;
    @CacheEvict(value = "xjfiletree" ,allEntries=true)
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody XjFile xjFile, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        xjFile.setCreateTime(new Date());
        xjFile.setCreator(token.getUser().getId().toString());
        boolean insert = xjFileService.insert(xjFile);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id , HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        boolean result = xjFileService.deleteById(id);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody XjFile xjFile, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        xjFile.setCreateTime(new Date());
        xjFile.setCreator(token.getUser().getId().toString());
        Boolean result = xjFileService.updateById(xjFile);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @CacheEvict(value = "xjfiletree" ,allEntries=true)
    @RequestMapping("updateTree")
    public SimpleCoreResponse updateTree(@RequestBody XjFile xjFile, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        xjFile.setCreateTime(new Date());
        xjFile.setCreator(token.getUser().getId().toString());
        Boolean result = xjFileService.updateById(xjFile);
        return  new SimpleCoreResponse("200","修改成功",result);
    }


    @RequestMapping("selectAll")
    public SimpleCoreResponse selectAll( HttpServletRequest request) throws AdscmSystemException{
        List<XjFile> xjFiles = xjFileService.selectAll();
        return  new SimpleCoreResponse("200","查询成功",xjFiles);
    }
    @RequestMapping("selectByName")
    public SimpleCoreResponse selectByName( HttpServletRequest request) throws AdscmSystemException{
        Wrapper<XjFile> wrapper =new EntityWrapper<>();
        wrapper.setSqlSelect("id","name","code");
        List<XjFile> xjFiles = xjFileService.selectList(wrapper);
        return  new SimpleCoreResponse("200","查询成功",xjFiles);
    }
    @RequestMapping("selectById/{id}")
    public SimpleCoreResponse selectById(@PathVariable  Integer id,  HttpServletRequest request) throws AdscmSystemException{
        XjFile xjFile = xjFileService.selectXjFile(id);
        return  new SimpleCoreResponse("200","查询成功",xjFile);
    }
}

